home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / W32SVSPY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  7.3 KB  |  277 lines

  1. //==================================
  2. // W32SVSPY - Matt Pietrek 1995
  3. // FILE: W32SVSPY.C
  4. //==================================
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <commdlg.h>
  8. #pragma hdrstop
  9. #include "w32spdll.h"
  10. #include "w32svspy.h"
  11. #include "w32info.h"
  12. #include "w32srvdb.h"
  13. #include "w32svflt.h"
  14.  
  15. // Prototype the functions for this
  16. void Handle_WM_COMMAND(HWND hWndDlg, WPARAM wParam, LPARAM lParam);
  17. void Handle_WM_INITDIALOG(HWND hWndDlg);
  18. BOOL CALLBACK W32SpyDlgProc(HWND, UINT, WPARAM, LPARAM);
  19. void StartUp( HWND hWndDlg );
  20. void CleanUp( HWND hWndDlg );
  21. void ShowResults(void);
  22. void SetButtonStates( HWND hWndDlg );
  23. void SaveToFile( HWND hWndDlg );
  24. BOOL FormatReportLine(  unsigned eventIndex,
  25.                         PWIN32SERVICECALLINFO pCallArray,
  26.                         PSTR pszBuffer,
  27.                         unsigned cbBuffer );
  28. BOOL IsFilteredCall( unsigned eventIndex, PWIN32SERVICECALLINFO pCallArray );
  29.  
  30.  
  31. //====================== Global Vars ======================================
  32. HINSTANCE HInstance;
  33.  
  34. HWND HWndMainList;
  35. HWND HWndStatus;
  36.  
  37. BOOL FIntercepted = FALSE;
  38.  
  39. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance,
  40.                     LPSTR lpszCmdLine, int nCmdShow )
  41. {
  42.     HInstance = hInstance;
  43.  
  44.     LoadSavedFilterValues();
  45.     
  46.     DialogBox(hInstance, "W32SPY_DLG", 0, (DLGPROC)W32SpyDlgProc);
  47.     
  48.     SaveFilterValues();
  49.     
  50.     return 0;
  51. }
  52.  
  53. void StartUp( HWND hWndDlg )
  54. {
  55.     if ( !FIntercepted )
  56.     {
  57.         SendMessage( HWndMainList, LB_RESETCONTENT, 0, 0 );
  58.         InitWin32ServiceSpyDLL();
  59.         FIntercepted = TRUE;
  60.         SetWindowText( HWndStatus, "Spying..." );
  61.         SetButtonStates( hWndDlg );
  62.     }
  63. }
  64.  
  65. void CleanUp( HWND hWndDlg )
  66. {
  67.     if ( FIntercepted )
  68.     {
  69.         StopWin32ServiceSpy();
  70.         FIntercepted = FALSE;
  71.         SetWindowText( HWndStatus, "disabled..." );
  72.         SetButtonStates( hWndDlg );
  73.     }
  74. }
  75.  
  76. void ShowResults(void)
  77. {
  78.     DWORD cCalls;
  79.     PWIN32SERVICECALLINFO pCallArray;
  80.     DWORD i;
  81.     char szBuffer[512];
  82.         
  83.     if ( !GetWin32ServiceLogInfo(&cCalls, &pCallArray) )
  84.         return;
  85.     
  86.     wsprintf( szBuffer, "%u calls logged", cCalls );
  87.     SetWindowText( HWndStatus, szBuffer );
  88.     
  89.     SendMessage( HWndMainList, LB_RESETCONTENT, 0, 0 );
  90.     SendMessage( HWndMainList, WM_SETREDRAW, FALSE, 0 );
  91.  
  92.     for ( i = 0; i < cCalls; i++ )
  93.     {
  94.         char szBuffer[512];
  95.         
  96.         if ( !IsFilteredCall( i, pCallArray ) )
  97.         {
  98.             FormatReportLine( i, pCallArray, szBuffer, sizeof(szBuffer) );
  99.             SendMessage( HWndMainList, LB_ADDSTRING, 0, (LPARAM)szBuffer );
  100.         }
  101.     }   
  102.  
  103.     SendMessage( HWndMainList, WM_SETREDRAW, TRUE, 0 );
  104. }
  105.  
  106. //
  107. // Dialog proc for the main dialog
  108. //
  109. BOOL CALLBACK W32SpyDlgProc(HWND hWndDlg, UINT msg,
  110.                               WPARAM wParam, LPARAM lParam)
  111. {
  112.     switch ( msg )
  113.     {
  114.         case WM_COMMAND:
  115.             Handle_WM_COMMAND(hWndDlg, wParam, lParam); return TRUE;
  116.         case WM_INITDIALOG:
  117.             Handle_WM_INITDIALOG(hWndDlg); return TRUE;
  118.         case WM_CLOSE:
  119.             CleanUp( hWndDlg );
  120.             EndDialog(hWndDlg, 0); return FALSE;
  121.     }
  122.     return FALSE;
  123. }
  124.  
  125. //
  126. // Handle the dialog's WM_COMMAND messages
  127. //
  128. void Handle_WM_COMMAND(HWND hWndDlg, WPARAM wParam, LPARAM lParam)
  129. {
  130.     switch ( LOWORD(wParam) )
  131.     {
  132.         case IDC_BUTTON_START:
  133.             StartUp( hWndDlg );
  134.             break;
  135.             
  136.         case IDC_BUTTON_STOP:
  137.             CleanUp( hWndDlg );
  138.             ShowResults();
  139.             break;
  140.             
  141.         case IDC_BUTTON_EXIT:
  142.             CleanUp( hWndDlg );
  143.             EndDialog(hWndDlg, 0);
  144.             break;
  145.  
  146.         case IDC_BUTTON_FILTER:
  147.             DialogBox(HInstance, "Win32ServicesFilterDialog", hWndDlg,
  148.                         (DLGPROC)W32SpyFilterDlgProc);
  149.             ShowResults();
  150.             break;
  151.  
  152.         case IDC_BUTTON_SAVE:
  153.             SaveToFile( hWndDlg );
  154.             break;
  155.     }
  156.  
  157.     return;
  158. }
  159.  
  160. void Handle_WM_INITDIALOG(HWND hWndDlg)
  161. {
  162.     HWndMainList = GetDlgItem(hWndDlg, IDC_LISTBOX_CALLS);
  163.     HWndStatus = GetDlgItem(hWndDlg, IDC_TEXT_STATUS);
  164.  
  165.     SetButtonStates( hWndDlg );
  166.     
  167.     SendMessage( HWndMainList, WM_SETFONT,
  168.                  (WPARAM)GetStockObject(OEM_FIXED_FONT), 0 );
  169. }
  170.  
  171. void SetButtonStates( HWND hWndDlg )
  172. {
  173.     EnableWindow( GetDlgItem(hWndDlg, IDC_BUTTON_START),
  174.                     FIntercepted ? FALSE : TRUE );
  175.     EnableWindow( GetDlgItem(hWndDlg, IDC_BUTTON_STOP),
  176.                     FIntercepted ? TRUE : FALSE );  
  177.     EnableWindow( GetDlgItem(hWndDlg, IDC_BUTTON_SAVE),
  178.                     FIntercepted ? FALSE : TRUE );  
  179. }
  180.  
  181. void SaveToFile( HWND hWndDlg )
  182. {
  183.     DWORD cCalls;
  184.     PWIN32SERVICECALLINFO pCallArray;
  185.     OPENFILENAME ofn;
  186.     char szFile[MAX_PATH];
  187.     FILE *pOutFile;
  188.     unsigned i;
  189.  
  190.     if ( !GetWin32ServiceLogInfo(&cCalls, &pCallArray) )
  191.         return;
  192.  
  193.     if ( cCalls == 0 )
  194.         return;
  195.  
  196.     // use COMMDLG.DLL to browse for the name to save as
  197.     memset(&ofn, 0, sizeof(OPENFILENAME));
  198.     szFile[0] = '\0';
  199.     ofn.lStructSize = sizeof(OPENFILENAME);
  200.     ofn.hwndOwner = hWndDlg;
  201.     ofn.lpstrFile= szFile;
  202.     ofn.nMaxFile = sizeof(szFile);
  203.     if ( !GetSaveFileName(&ofn) )
  204.         return;
  205.  
  206.     pOutFile = fopen((LPSTR)ofn.lpstrFile, "wt");
  207.     if ( !pOutFile )
  208.     {
  209.         MessageBox(hWndDlg,"Couldn't open specified report file", 0, MB_OK);
  210.         return;
  211.     }
  212.  
  213.     fprintf( pOutFile, "%u calls\n", cCalls );
  214.     for ( i = 0; i < cCalls; i++ )
  215.     {
  216.         char szBuffer[1024];
  217.         
  218.         if ( !IsFilteredCall( i, pCallArray ) )
  219.         {
  220.             FormatReportLine( i, pCallArray, szBuffer, sizeof(szBuffer) );
  221.             fputs( szBuffer, pOutFile );
  222.             fputs( "\n", pOutFile );
  223.         }
  224.     }   
  225.  
  226.     fclose(pOutFile);   
  227. }
  228.  
  229. BOOL
  230. FormatReportLine(
  231.             unsigned eventIndex,
  232.             PWIN32SERVICECALLINFO pCallArray,
  233.             PSTR pszBuffer,
  234.             unsigned cbBuffer )
  235. {
  236.     char szBuffer2[512];
  237.     
  238.     GetWin32ServiceName( pCallArray[eventIndex].serviceId,
  239.                         pCallArray[eventIndex].param1,
  240.                         szBuffer2,
  241.                         sizeof(szBuffer2) );
  242.  
  243.     if ( pCallArray[eventIndex].szName[0] == 0 )
  244.     {
  245.         char szTemp[10];
  246.         wsprintf(szTemp, "%08X", pCallArray[eventIndex].processId);
  247.         memcpy( pCallArray[eventIndex].szName, szTemp, 8 );
  248.     }
  249.     
  250.     #if 0
  251.     wsprintf( pszBuffer, "%08X (p1=%08X) %s",
  252.                 pCallArray[eventIndex].serviceId,
  253.                 pCallArray[eventIndex].param1,
  254.                 szBuffer2 );
  255.     #else
  256.     wsprintf( pszBuffer, "%-8.8s %s(%08X)",
  257.                 pCallArray[eventIndex].szName,
  258.                 szBuffer2,
  259.                 pCallArray[eventIndex].param1 );
  260.     #endif
  261.         
  262.     return TRUE;
  263. }
  264.  
  265. BOOL IsFilteredCall( unsigned eventIndex, PWIN32SERVICECALLINFO pCallArray )
  266. {
  267.     PWIN32_SERVICE_CALL pWin32ServiceCall;
  268.  
  269.     pWin32ServiceCall =
  270.         LookupWin32ServiceCall( pCallArray[eventIndex].serviceId );
  271.     
  272.     if ( !pWin32ServiceCall )
  273.         return FALSE;       // If we don't know about it, don't filter it
  274.     
  275.     return pWin32ServiceCall->fIgnore;
  276. }
  277.